package com.huabo.model;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.huabo.base.BaseModel;
import com.huabo.utils.DateUtils;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "TC_OPERATION_LOG")
public class OperationLog extends BaseModel implements Serializable {

    /**
     * <p>Field serialVersionUID: 序列号</p>
     */
    private static final long serialVersionUID = 1L;

    //主键
    @Id
    @Column(name = "F_ID")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    //应用系统/资源库标识
    @Column(name = "F_REG_ID")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long regId;

    //用户标识
    @Column(name = "F_USER_ID")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long userId;

    //用户单位
    @Column(name = "F_ORG_NAME")
    private String orgName;

    //用户单位组织机构代码
    @Column(name = "F_ORG_CODE")
    private String orgCode;

    //用户名称
    @Column(name = "F_USER_NAME")
    private String userName;

    //操作时间
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "F_OPERATE_TIME")
    private Date operateTime;

    //终端标识(用户操作时所使用的信息处理终端的唯一标识；对于直接接入公安网使用的信息处理终端，填写其网络ip地址，如：10.2.64.71；移动警务终端通过网络安全边界接入设备接入公安网的，填写该终端的硬件序列号)
    @Column(name = "F_TERMINAL_ID")
    private String terminalId;

    //操作类型(1：查询；2：新增；3：修改；4：删除)
    @Column(name = "F_OPERATE_TYPE")
    private Integer operateType;

    //操作结果(1:成功；0：失败)
    @Column(name = "F_OPERATE_RESULT")
    private Integer operateResult;

    //失败原因代码(当操作结果为失败时，可记录操作失败的原因代码，采用附录a：用户操作失败原因代码)
    @Column(name = "F_ERROR_CODE")
    private String errorCode;

    //功能模块名称(操作类型为0-登录时，置空；为其它类型时，可填写当时用户所操作的具体功能模块名称)
    @Column(name = "F_FUNCTION_NAME")
    private String functionName;

    //操作条件(为其它类型时，可记录用户进行操作时的数据筛选条件，填写数据操作sql语句的where子句内容，如：name=‘张三’)
    @Column(name = "F_OPERATE_CONDITION")
    private String operateCondition;

    //日志对象类型
    @Column(name = "F_OPERATE_OBJECT")
    private String operateObject;

    //日志对象名称(比如“暂住人口表”)
    @Column(name = "F_TABLE_NAME")
    private String tableName;
    
    //资源名称
    @Column(name = "F_RESOURCE_NAME")
    private String resourceName;

    //资源编码
    @Column(name = "F_RESOURCE_CODE")
    private String resourceCode;
    
    //访问路径
    @Column(name = "F_RESOURCE_URL")
    private String resourceUrl;
    
    public String getOperateTimeStr() {
		if (operateTime != null) {
			return DateUtils.dateToString(operateTime, "yyyy-MM-dd HH:mm:ss");
		}
		return null;
	}
    
    // 格式化操作类型
 	public String getOperateTypeStr() {
 		return OperateType.valueOf(operateType);
 	}

 	
    public String getResourceName() {
		return resourceName;
	}

	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}

	public String getResourceCode() {
		return resourceCode;
	}

	public void setResourceCode(String resourceCode) {
		this.resourceCode = resourceCode;
	}

	public String getResourceUrl() {
		return resourceUrl;
	}

	public void setResourceUrl(String resourceUrl) {
		this.resourceUrl = resourceUrl;
	}

	public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getRegId() {
        return regId;
    }

    public void setRegId(Long regId) {
        this.regId = regId;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getOrgName() {
        return orgName;
    }

    public void setOrgName(String orgName) {
        this.orgName = orgName;
    }

    public String getOrgCode() {
        return orgCode;
    }

    public void setOrgCode(String orgCode) {
        this.orgCode = orgCode;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Date getOperateTime() {
        return operateTime;
    }

    public void setOperateTime(Date operateTime) {
        this.operateTime = operateTime;
    }

    public String getTerminalId() {
        return terminalId;
    }

    public void setTerminalId(String terminalId) {
        this.terminalId = terminalId;
    }

    public Integer getOperateType() {
        return operateType;
    }

    public void setOperateType(Integer operateType) {
        this.operateType = operateType;
    }

    public Integer getOperateResult() {
        return operateResult;
    }

    public void setOperateResult(Integer operateResult) {
        this.operateResult = operateResult;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getOperateCondition() {
        return operateCondition;
    }

    public void setOperateCondition(String operateCondition) {
        this.operateCondition = operateCondition;
    }

	public String getFunctionName() {
		return functionName;
	}

	public void setFunctionName(String functionName) {
		this.functionName = functionName;
	}

	public String getOperateObject() {
		return operateObject;
	}

	public void setOperateObject(String operateObject) {
		this.operateObject = operateObject;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public enum OperateType {
		CHECK(1, "查询"), ADD(2, "新增"), MODIFY(3, "修改"), DELETE(4, "删除");

		private int value;
		private String lable;

		private OperateType(int value, String lable) {
			this.value = value;
			this.lable = lable;
		}

		public static String valueOf(int value) {
			OperateType[] values = OperateType.values();
			for (OperateType resourceEnum : values) {
				if (value == resourceEnum.getValue()) {
					return resourceEnum.getLable();
				}
			}
			return null;
		}

		public int getValue() {
			return value;
		}

		public String getLable() {
			return lable;
		}

	}

}